//
//  MenuView.m
//  JKSideSlipView
//
//  Created by Jakey on 15/1/10.
//  Copyright (c) 2015年 www.skyfox.org. All rights reserved.
//

#import "MenuView.h"

//#import "MenuCell.h"
@implementation MenuView
+(instancetype)menuView
{
    UIView *result = nil;

    NSArray* nibView =  [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
    for (id object in nibView)
    {
        if ([object isKindOfClass:[self class]])
        {
            result = object;
            
            break;
        }
    }
    return result;
}
-(instancetype)init
{
    self=[super init];
    
    return self;
}
- (IBAction)homeBtnClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(menuViewHomeBtnClick)]) {
        
        [self.delegate menuViewHomeBtnClick];
    }

}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if (buttonIndex == 1) {
        if ([self.delegate respondsToSelector:@selector(menuViewConfirmCancelClick)]) {
            
            [self.delegate menuViewConfirmCancelClick];
        }
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user removeObjectForKey:@"tokenId"];
        
        [user synchronize];
        
    }
    
}


- (IBAction)cancelBtnClick:(id)sender {
    UIAlertView* alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"是否确认注销？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
    [alt show];
}
-(void)didSelectRowAtIndexPath:(void (^)(id cell, NSIndexPath *indexPath))didSelectRowAtIndexPath{
   
    _didSelectRowAtIndexPath = [didSelectRowAtIndexPath copy];
}
-(void)setItems:(NSArray *)items{
    _items = items;
  
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSMutableArray* funConfigs = [user objectForKey:@"funConfigs"];
    [subArray removeAllObjects];
    [subIconArray removeAllObjects];
    subArray =[NSMutableArray array];
    subIconArray =[NSMutableArray array];
    parentArray=nil;
    

    NSArray *yingXiaoSubArray=[NSArray arrayWithObjects: @"扫码入会", @"会员管理",@"新媒体推广" ,nil];
    [subArray addObject:yingXiaoSubArray];
    NSArray *yingXiaoIconArray=[NSArray arrayWithObjects:
                                @"dianpuerweima",
                                
                                @"huiyuan",
                                @"xinmeitituiguang",

                                nil];
    [subIconArray addObject:yingXiaoIconArray];
    
    NSArray *xiaoShouSubArray=[NSArray arrayWithObjects:@"下单收银",@"销售订单",@"提货配送",@"销售退货",nil];
    [subArray addObject:xiaoShouSubArray];
    NSArray *xiaoShouIconArray=[NSArray arrayWithObjects:@"shouyin",
                                @"xiaoshoudingdan",
                                @"tihuopeisong",
                                 @"xiaoshoutuihuo_new",
                                nil];
    [subIconArray addObject:xiaoShouIconArray];
    
    
    
    NSArray *kuCunSubArray=[NSArray arrayWithObjects:@"要货订单",@"商品入库",@"商品出库",@"库存盘点",@"存货管理",@"调拨单",@"进货单",nil];
    [subArray addObject:kuCunSubArray];
    
    
    NSArray *kuCunIconArray=[NSArray arrayWithObjects:       @"yaohuoguanli",
                             @"shangpinruku",
                             @"shangpinchuku",
                             @"kucunpandian",
                             @"cunhuoguanli",
                             @"diaobodan",
                             @"jinhuodan",
                             nil];
    [subIconArray addObject:kuCunIconArray];
    
    
    parentArray =[NSMutableArray arrayWithObjects:@"营销管理",@"销售管理",@"库存管理", nil];
    
    
    if (funConfigs.count>0) {
        for (NSDictionary* dicc in funConfigs )
        {
            
            if ([[NSString stringWithFormat:@"%@",[dicc valueForKey:@"display"] ]isEqualToString:@"1"]&&[[NSString stringWithFormat:@"%@",[dicc valueForKey:@"funCode"]]isEqualToString:@"menu_booking_list"]){
                
                parentArray=nil;
                parentArray =[NSMutableArray arrayWithObjects:@"营销管理",@"销售管理",@"库存管理",@"预约管理", nil];
                NSArray *yuyueSubArray=[NSArray arrayWithObjects:@"预约单",nil];
                [subArray addObject:yuyueSubArray];
                
                
                NSArray *yuyueIconArray=[NSArray arrayWithObjects: @"yuyuedan",
                                         
                                         nil];
                [subIconArray addObject:yuyueIconArray];
                
                
            }
        }
        
    }
    [_myTableView setDataSourceDelegate:self];
    [_myTableView setTableViewDelegate:self];

    
}


#pragma mark - SubTableViewDelegate

// @optional

- (void)tableView:(UITableView *)tableView didSelectCellAtChildIndex:(NSInteger)childIndex withInParentCellIndex:(NSInteger)parentIndex {
//    
//    if ( [self.delegate respondsToSelector:@selector(didSelectCellAtChildIndex:withInParentCellIndex:)]) {
//        
//        [self.delegate didSelectCellAtChildIndex:childIndex withInParentCellIndex:parentIndex];
//    }
    
}
-(void)subTableviewCellSubtractOrAddClick
{
   [self.delegate subTableviwAddOrSubtractClickAtHome];
}
- (void)childDidSelectCellAtChildIndex:(NSInteger)childIndex withInParentCellIndex:(NSInteger)parentIndex
{
    if ( [self.delegate respondsToSelector:@selector(didSelectCellAtChildIndex:withInParentCellIndex:)]) {
        
        [self.delegate didSelectCellAtChildIndex:childIndex withInParentCellIndex:parentIndex];
    }
}
- (void)tableView:(UITableView *)tableView didDeselectCellAtChildIndex:(NSInteger)childIndex withInParentCellIndex:(NSInteger)parentIndex {
    // ...
}
- (void)tableView:(UITableView *)tableView didSelectParentCellAtIndex:(NSInteger)parentIndex {
      
}


#pragma mark - SubTableDataSource - Parent

// @required
- (NSInteger)numberOfParentCells {
    
    return parentArray.count;
}
- (NSInteger)heightForParentRows {
    
    return 50;
}

// @optional
- (NSString *)titleLabelForParentCellAtIndex:(NSInteger)parentIndex {
    
    return [NSString stringWithFormat:@"%@",[parentArray objectAtIndex:parentIndex]];
}
- (NSString *)subtitleLabelForParentCellAtIndex:(NSInteger)parentIndex {
    
    return nil;


}



#pragma mark - SubTableDataSource - Child

// @required
- (NSInteger)numberOfChildCellsUnderParentIndex:(NSInteger)parentIndex {
   
//    for(int i=0;i<subArray.count;i++)
//    {
//        NSLog(@"subArray[%d]=======%@",parentIndex,subArray[parentIndex]);
//    }
    return [[subArray objectAtIndex:parentIndex] count];
}
- (NSInteger)heightForChildRows {
    
    return 49;
}

// @optional
- (NSString *)titleLabelForCellAtChildIndex:(NSInteger)childIndex withinParentCellIndex:(NSInteger)parentIndex {
    NSArray *oneSubArray=[subArray objectAtIndex:parentIndex];
    NSString *subTitleStr=[oneSubArray objectAtIndex:childIndex];
    
    return subTitleStr;

    //return [NSString stringWithFormat:@"Child %i",childIndex + 1];
}
- (UIImage *)iconForCellAtChildIndex:(NSInteger)childIndex withinParentCellIndex:(NSInteger)parentIndex
{
    NSArray *oneSubIconArray=[subIconArray objectAtIndex:parentIndex];
    NSString *subIconStr=[oneSubIconArray objectAtIndex:childIndex];
     
    UIImage *img=[UIImage imageNamed:subIconStr];
    
    UIImage *tinted = [img rt_tintedImageWithColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1.0] ];
    
    
   // UIImage *tinted = [img rt_tintedImageWithColor:[UIColor colorWithRed:0.008 green:0.62 blue:0.64 alpha:1.0]  level:0.5];
    //[imgView setImage:tinted];

    return tinted;
}
- (NSString *)subtitleLabelForCellAtChildIndex:(NSInteger)childIndex withinParentCellIndex:(NSInteger)parentIndex {
    return @"";
 //   return [NSString stringWithFormat:@"nested under parent %i",parentIndex + 1];
//    NSArray *oneSubArray=[subArray objectAtIndex:parentIndex];
//    NSString *subTitleStr=[oneSubArray objectAtIndex:childIndex];
//    //    if (childCount > 1)
//    //        return [NSString stringWithFormat:@"%i children", (int)childCount];
//    //    else if (childCount == 1)
//    //        return [NSString stringWithFormat:@"%i child", (int)childCount];
//    //    else
//    //        return @"No children";
//    return subTitleStr;

}



@end
